package com.nimitz.web.controller.module.demo;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.utils.StringUtils;
import com.nimitz.module.demo.domain.DemUser;
import com.nimitz.module.demo.service.IDemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户维护
 */
@RestController
@Api(tags = "演示模块-用户维护")
@RequestMapping("/module/demo/user")
public class DemUserController extends BaseController {

    @Resource
    private IDemUserService userService;

    /**
     * 新增用戶
     *
     * @param user 用戶
     * @return 结果
     */
    @PostMapping
    @ApiOperation(value = "新增用户")
    // @Log(title = "用户管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody DemUser user) {
        user.setCreateBy("Vincent.Jin");
        return toAjax(userService.insertUser(user));
    }

    /**
     * 根据用户id删除用户
     *
     * @param userId 用户id
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userId}")
    @ApiOperation(value = "根据用户id删除用户")
    public AjaxResult remove(@PathVariable Integer userId) {
        return toAjax(userService.deleteUserById(userId));
    }

    /**
     * 批量删除用戶
     *
     * @param userIds 需要删除的用戶id集合
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/userIds/{userIds}")
    @ApiOperation(value = "批量删除用户")
    public AjaxResult removes(@PathVariable Integer[] userIds) {
        return toAjax(userService.deleteUserByIds(userIds));
    }

    /**
     * 修改用戶
     *
     * @param user 用戶
     * @return 结果
     */
    // @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改用戶")
    public AjaxResult edit(@RequestBody DemUser user) {
        user.setUpdateBy("Vincent.Jin");
        return toAjax(userService.updateUser(user));
    }

    /**
     * 查询所有用户
     *
     * @return 用戶集合
     */
    @GetMapping("/list/all")
    @ApiOperation(value = "查询所有用户")
    public TableDataInfo list() {
        startPage();
        List<DemUser> list = userService.selectUserAll();
        return getDataTable(list);
    }

    /**
     * 根据用戶id查询用戶
     *
     * @param userId 用戶id
     * @return 用戶
     */
    @GetMapping("/{userId}")
    @ApiOperation(value = "根据用戶id查询用戶")
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Integer userId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId)) {
            DemUser demUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, demUser);
        }
        return ajax;
    }

    /**
     * 根据条件查询用户列表（含分页）
     *
     * @param user 用戶
     * @return 用戶集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询用户列表（含分页）")
    public TableDataInfo list(DemUser user) {
        startPage();
        List<DemUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }
}
